package com.example.kotlintest.net

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.example.kotlintest.bean.TouTiaoData
import com.example.kotlintest.bean.WeatherData
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class DataRepository @Inject constructor(
    private val api: ApiService
) {

    suspend fun getWeatherData(cityname: String, key: String): WeatherData {
        return api.weatherInfo(cityname, key)
    }

    fun getToutiaoData(): Flow<PagingData<TouTiaoData.Result.Data>> {
        return Pager(
            config = PagingConfig(
                prefetchDistance = 1,
                pageSize = 30
            ),
            pagingSourceFactory = { NewsPagingSource(api) }
        ).flow
    }
}